package com.tim.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tim.pojo.Cell;
import com.tim.pojo.DataResult;
import com.tim.service.CellService;
import com.tim.service.Impl.CellServcieImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者：Tim
 * 时间：2021/4/28 11:25
 */
@WebServlet("/cell/*")
public class CellServlet extends BaseServlet{
    CellService cellService = new CellServcieImpl();

    /**
     * 分页查询所有
     */
    public DataResult all(HttpServletRequest request,HttpServletResponse response){
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        String plotName = request.getParameter("plotName");
        DataResult dataResult = cellService.limit(page,limit,plotName);
        return dataResult;
    }
    /**
     * 删除
     */
    public DataResult del(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        int count = cellService.deleteCell(id);
        DataResult dataResult;
        if(count>0){
            dataResult= new DataResult(0,"删除成功");
        }else{
            dataResult= new DataResult(40000,"删除成功");
        }
        return dataResult;
    }

    /**
     * 添加
     */
    public DataResult add(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String info = request.getParameter("info");
        //将前端传递的json字符串转换成java对象
        ObjectMapper mapper = new ObjectMapper();
        Cell cell = mapper.readValue(info, Cell.class);
        int count = cellService.addCell(cell);
        DataResult dataResult;
        if(count>0){
            dataResult = new DataResult(0,"添加成功");
        }else{
            dataResult= new DataResult(40000,"添加失败");
        }
        return dataResult;
    }
    /**
     * 根据ID进行查询
     */
    public DataResult one(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        Cell cell = cellService.selectOne(id);
        DataResult dataResult;
        if (cell==null){
            dataResult = new DataResult(40000,"查无结果");
        }
        dataResult = new DataResult(0,"查询成功",cell);
        return dataResult;
    }
    /**
     * 修改编辑
     */
    public DataResult update(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String info = request.getParameter("info");
        ObjectMapper mapper = new ObjectMapper();
        Cell cell = mapper.readValue(info, Cell.class);
        int count  = cellService.updateCell(cell);
        DataResult dataResult;
        if(count>0){
            dataResult = new DataResult(0,"修改成功");
        }else{
            dataResult=new DataResult(40000,"修改失败");
        }
        return dataResult;
    }

    /**
     * 批量删除
     */
    public DataResult delAll(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //1、获取请求参数（ids） JSON字符串（数组）
        String ids = request.getParameter("ids");
        ObjectMapper mapper = new ObjectMapper();
        String[] idArr = mapper.readValue(ids, String[].class);

        int count = cellService.deleteAll(idArr);
        DataResult dataResult;
        if(count>0){
            dataResult = new DataResult(0,"删除成功");
        }else{
            dataResult = new DataResult(40000,"删除失败");
        }
        return dataResult;
    }
}
